C中Struct和Typedef Struct的前向声明及区别 |
您所在的位置:网站首页 › typedef struct SORTTYPE › C中Struct和Typedef Struct的前向声明及区别 |
当前位置:主页 > 学无止境 > 编程语言 > C语言 >
Python
PHP
Java
Go
TypeScript
C++
Vba
Node.js
C语言
MATLAB
C中Struct和Typedef Struct的前向声明及区别
作者:迹忆客
最近更新:2023/05/05
浏览次数:
本篇文章将解释 struct 的概念和 typedef 关键字的使用。 我们还将在 C 中看到前向声明的概念。 让我们从创建一个 C 项目开始。 创建一个 C 项目 第一步是安装编译器。 下载和安装 C 编译器的步骤。 在下一步中,通过单击菜单栏中的文件,用 C 语言创建一个空项目。
编译前保存文件。 执行代码。 通过点击编译和运行。 将出现一个执行屏幕。 C语言中的结构体 struct 在C语言中用于对相关的数据成员进行分组。 结构中的成员没有相同的数据类型,并且该组包含保存在一个地方的不同变量。 正如我们所知,该数组具有相同类型的成员,但在结构中,数据成员可以是不同类型,如 int 、float 和 char。 下面的代码片段将描述如何用 C 语言创建一个结构体。 在这段代码中,我们通过编写结构名称及其变量在 main 函数的局部范围内声明了变量。 代码示例: #include struct Student { // Structure declaration int rollno; // Member (int variable) char gender; // Member (char variable) }; // End the structure with a semicolon int main() { struct Student s1; // Making a variable of a struct s1.rollno = 3; // assigning value to every member using dot operator s1.gender = 'M'; printf("The name and gender of a student is %d and %c", s1.rollno, s1.gender); }输出: The name and gender of a student is 3 and M我们将在以下代码中声明全局范围内的变量。 代码示例: #include struct Student { // Structure declaration int rollno; // Member (int variable) char gender; // Member (char variable) } s1; // Declared a variable s1 before ending the struct with a semicolon int main() { s1.rollno = 3; // calling a member using a variable s1.gender = 'M'; printf("The name and gender of a student is %d and %c", s1.rollno, s1.gender); }输出: The name and gender of a student is 3 and M同样,这将给出相同的输出,但在上面的代码片段中,我们已经全局声明了变量 s1。 我们不必在 main() 函数中定义结构。 C 中的 typedefC 语言中使用 typedef 将我们现有的数据类型重命名为新数据类型。 语法: typedef首先我们要写 typedef 的保留字,然后是C语言中已有的名字,还有我们要赋值的名字。 使用 typedef 后,我们将在整个程序中使用 alias_name。 代码: #include void main() { typedef int integer; // assigning int a new name of the integer integer a= 3; // variable is declared by integer integer b= 4; printf("The values of a and b are %d and %d", a,b); // print value of a and b on the screen }输出: The values of a and b are 3 and 4 C 中的 typedef 结构我们已经看到,我们必须在 main() 函数中编写整个结构定义。 我们可以使用 typedef 将旧类型替换为新类型,而不是每次都编写 struct student。 Typedef 帮助我们用 C 语言创建我们的类型。 代码示例: #include // including header file of input/output #include // including header file of string typedef struct Books { // old type char title[30]; // data members of the struct book char author[30]; char subject[50]; int id; } Book; // new type void main() { Book b1; // variable of a new type strcpy( b1.title, "C Programming"); // copy string in title strcpy( b1.author, "Robert Lafore"); // copy string in author strcpy( b1.subject, "Typedef Struct in C"); // copy string in subject b1.id = 564555; // assigning id to variable printf( "Book title is : %s\n", b1.title); // printing book title on screen printf( "Book author is : %s\n", b1.author); printf( "Book subject is : %s\n", b1.subject); printf( "Book id is : %d\n", b1.id); }输出: Book title is : C Programming Book author is : Robert Lafore Book subject is : Typedef Struct in C Book id is : 564555在上面的代码中,我们在分号之前的结构末尾定义了一个新的结构类型。 然后,我们在 main() 函数中使用了这个新类型。 这个 typedef 减少了每次在 main() 函数中定义 struct 来创建变量的工作量。 C 中的前向声明前向声明是在 Struct 的实际定义之前的声明。 定义不可用,但由于前向声明,我们可以引用声明的类型,这是一种预先声明。 此方法用于定义和声明函数。 我们可以在顶部声明它并在底部定义它,而不是在 main() 函数之上定义一个函数,称为前向声明。 代码示例: #include int add(int x , int y); // (prototype)function declaration void main() { int n1,n2,sum; scanf("%d %d", &n1, &n2); // taking numbers from user sum= add(n1,n2); // call to the add function printf("The sum of n1 and n2 is %d ", sum); // display sum on the screen } int add(int x , int y) // function definition { int result; result= x+y; return result; // returning the result to the main function }上面的代码片段表明我们已经在 main() 函数的顶部声明了一个函数原型。 我们在定义它之前在 main 中调用了 add() 函数,但是它可以正常工作,因为原型已经被定义了。 这是由于前向声明。 输出: 4 5 The sum of n1 and n2 is 9上一篇:C 中的 %G 格式说明符 下一篇:没有了 转载请发邮件至 [email protected] 进行申请,经作者同意之后,转载请以链接形式注明出处 本文地址: 相关文章 C 中的 %G 格式说明符发布时间:2023/05/06 浏览次数:117 分类:C语言 本文介绍了 C 语言中的格式说明符 %g 说明符。C 编程语言中的格式说明符。格式说明符只不过是一种特殊的字符串或运算符,可以在 C 中使用。 C 中的多个管道发布时间:2023/05/06 浏览次数:176 分类:C语言 本文演示了 C 中多管道的实现。C 中的管道概述。管道是两个不同进程之间链接的抽象表示,其中从一个进程返回的标准输出成为另一个进程的标准输入。 C 中移位运算符的类型发布时间:2023/05/06 浏览次数:146 分类:C语言 本文介绍如何在C中使用右移算术运算符,右移一个位置将每位向右移动一位。 >> 运算符是移位无符号值时的逻辑移位。 >> 运算符是一个算术 C 中的逻辑异或运算符发布时间:2023/05/06 浏览次数:157 分类:C语言 本文介绍 C 编程语言中的逻辑 XOR 运算符。C 中异或 (^) 运算符概述 异或,也称为异或,是一种逻辑运算符,当其中一个操作数为真(一个为真,另一个为假)时返回真结果,但当两个都为真或 C 中的 restrict 关键字发布时间:2023/05/06 浏览次数:51 分类:C语言 浅谈C中的restrict关键字。本篇文章将讨论 C 中的 restrict 关键字及其在编译器优化中的用途。 因此,我们将首先讨论编译器优化。 C语言二进制转十进制发布时间:2023/05/06 浏览次数:177 分类:C语言 本文全面讨论了将二进制数转换为十进制数的多个 C 实现。C语言二进制转十进制 二进制数只能包含两个值:0 和 1。但是,十进制表示法有十个符号:0 到 9 (0,1,2,3,4,5,6,7,8,9)。 在 C 中创建格式化字符串发布时间:2023/05/06 浏览次数:65 分类:C语言 这是一篇关于在 C 编程语言中格式化字符串的紧凑文章。本文讨论如何使用 C 语言格式化字符串,就像我们在 printf() 函数中所做的那样。 C 语言中的 bzero 函数发布时间:2023/03/30 浏览次数:106 分类:C语言 本文介绍了如何使用 C 语言中的 bzero 函数。 在 C 语言中使用原子类型发布时间:2023/03/30 浏览次数:104 分类:C语言 本文介绍如何在 C 语言中使用原子类型 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |